<?php

namespace app\stores\controller\miniapp;

use app\BaseController;
use app\beautiful\logic\LogsLogic;
use app\beautiful\logic\PartnerLogic;
use app\beautiful\model\CouponModel;
use app\beautiful\model\goods\GoodsGroupDetailModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\GoodsOrderItemModel;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\PartnerModel;
use app\common\logic\DataLogic;
use app\common\model\ShopImgModel;
use app\common\model\ShopModel;
use app\stores\logic\ConDataLogic;
use app\stores\model\member\MemberLevelModel;


class Shop extends BaseController{
    /*店铺信息*/
    public function info(){
        $lng =  $this->request->param('lng/f',0);
        $lat =  $this->request->param('lat/f',0);
        $shop = ShopModel::info(['shop_id'=>$this->request->shopId]);
        $shop['background_img'] = ShopImgModel::where(['shop_id'=>$this->request->shopId,'type'=>1])->find();
        $shop['distance'] = $shop->detail?get_distance($lat,$lng,$shop->detail->t_lat,$shop->detail->t_lng):0;
        $shop['is_open'] = 0;
        $business_time = $shop->detail->business_time;
        //是否营业中
        if($business_time){
            for ($i=0;$i<count($business_time);$i++){
                $start = strtotime(date('Y-m-d').' '.$business_time[$i]['start'].':00');
                $end = strtotime(date('Y-m-d').' '.$business_time[$i]['end'].':00');
                $now = time();
                if($now>=$start && $now<=$end){
                    $shop['is_open'] = 1;
                }
            }
        }
        //保存用户门店中等级信息
        if($this->request->shopId && $this->request->memberId){
            saveMemberLevel($this->request->shopId,$this->request->memberId);
        }

        return $this->success($shop);
    }
    /*拼团、秒杀*/
    public function activities(){

        $ConDataLogic = new ConDataLogic();
        //拼团
        $pinTuan = $ConDataLogic->getData(['shop_id'=>$this->request->shopId,'key'=>'pintuan','num'=>3]);
        //秒杀
        $flashSale=$ConDataLogic->getData(['shop_id'=>$this->request->shopId,'key'=>'miaosha','num'=>3]);

        if($pinTuan['pintuan']){
            $pinTuan['pintuan'] = addTwoData($pinTuan['pintuan'],['type'=>1]);
        }

        if($flashSale['miaosha']){
            $flashSale['miaosha'] = addTwoData($flashSale['miaosha'],['type'=>2]);
        }


        $data = array_merge($pinTuan['pintuan'],$flashSale['miaosha']);

        return $this->success($data);

    }
    /*超值套餐*/
    public function packageList(){
        $member = $this->request->member;
        $where = [
            ['shop_id','=',$this->request->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['type','=',3],
            ['online_sales','=',1],
        ];

        $order=['sort'=>'desc','add_time'=>'desc'];

        $goodsModel = GoodsModel::with(['goodsGroup'=>function($query){

            $query->where(['is_delete'=>0]);

        }])->json(['discount_detail'],true);

        $field = "goods_id,name,original_price,cover_img,vip_discount_type,discount_detail,type,total_value";

        $append = [];

        $data = DataLogic::getDataList($goodsModel,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        $list = $data['list'];

        if(!$member && $this->request->memberId){
            $member = MemberModel::find($this->request->memberId);
        }

        foreach ($list as $k=>$v){
            $list[$k]['order_count'] = GoodsOrderItemModel::with(['order'=>function($query)use ($v){

                $query->where(['shop_id'=>$v['shop_id'],'goods_id'=>$v['goods_id'],'is_paid'=>1]);

            }])->where(['shop_id'=>$v['shop_id'],'goods_id'=>$v['goods_id']])->group('order_id')->count();

            $goods_group = $v['goodsGroup'];
            $goods_group_ids = array_column($goods_group, 'goods_group_id');
            $where = [
                ['shop_id','=',$v['shop_id']],
                ['is_delete','=',0],
                ['goods_group_id','in',$goods_group_ids]
            ];
            $list[$k]['detail_str'] = $this->goodsGroupDetail($where);
            //折扣
            $return = getMemberPrice($member,$this->request->shopId,$v,$v['total_value'],$v['type']);
            $list[$k]['price'] = $return['price'];
            $list[$k]['discount'] = sprintf("%.2f",($v['original_price']/$v['total_value'])*10);

        }
        $data['list'] = $list;
        return $this->success($data);
    }
    /*产品推荐*/
    public function recommendGoodsList(){
        $member=$this->request->member;
        if(!$member && $this->request->memberId){
            $member = MemberModel::find($this->request->memberId);
        }

        $where['shop_id'] = $this->request->shopId;
        $where['status'] = 1;
        $where['type'] = 2;
        $where['is_tui'] = 1;
        $where['online_sales'] = 1;
        $order = ['g.sort'=>'desc','g.add_time'=>'desc'];

        $field = "g.sku_info,g.is_by_sales,g.origin_type,g.type,g.shop_id,g.goods_id,g.shop_id,g.cover_img,g.name,g.original_price,g.crossed_price,g.vip_price,g.quota,g.take_count,g.fictitious_take_count,g.category_id,g.discount_detail,g.vip_discount_type,
        g.type,sd.group_detail_name as unit,g.start_sales_num";
        $return = GoodsModel::list($where,$order,1,20,$field);

        $goods = $return['list'];

        foreach ($goods as $k=>$v){
            $return = getMemberPrice($member,$this->request->shopId,$v,$v['original_price'],2);
            $goods[$k]['price'] = $return['price'];
            $goods[$k]['discount'] = $return['discount'];
            if($v['sku']){
                $sku = skuMemberPrice($v['vip_discount_type'],$v['sku'],$member,$this->request->shopId,2);
                $goods[$k]['sku'] = $sku;
            }
        }
        return $this->success($goods);
    }

    /*获取套餐明细*/
    protected function goodsGroupDetail($where){
        $detail = GoodsGroupDetailModel::with(['goods'=>function($query){
            $query->where(['status'=>1,'is_delete'=>0]);
        }])->where($where)->select()->toArray();
        $detail_str = '';
        foreach ($detail as $k=>$v){
            if($v['goods']){
                $detail_str.=$v['goods']['name'];
            }
        }
        return $detail_str;
    }
}

